<style>

</style>
<div class="layuimini-container layuimini-page-anim">
    <div class="layuimini-main layui-top-box">

        <fieldset class="layui-elem-field layui-field-title" style="margin-top: 20px;">
            <legend>设置签到</legend>
        </fieldset>

        <form class="layui-form layui-form-pane" action="" lay-filter="setCheckin">

            <div class="layui-row layui-col-space20">

                <div class="layui-col-xs12 layui-col-md8 layui-col-md-offset2">
                    <!--教师工号-->
                    <div class="layui-form-item">
                        <label class="layui-form-label">教师姓名：</label>
                        <div class="layui-input-block">
                            <input type="text" name="teacherName" lay-verify="title" autocomplete="off" class="layui-input" disabled>
                        </div>
                    </div>

                    <!--课程名-->
                    <div class="layui-form-item">
                        <label class="layui-form-label">选择课程：</label>
                        <div class="layui-input-block">
                            <select name="courseId" lay-verify="required" id="course">
                                <option value=""></option>
                            </select>
                        </div>
                    </div>
                    <!--教室名-->
                    <div class="layui-form-item">
                        <label class="layui-form-label">选择教室：</label>
                        <div class="layui-input-block">
                            <select name="classroomId" lay-verify="required" id="classroom">
                                <option value=""></option>
                            </select>
                        </div>
                    </div>
                    <!--结束时间-->
                    <div class="layui-form-item">
                        <label class="layui-form-label">结束时间：</label>
                        <div class="layui-input-block">
                            <input type="text" name="finishtime" lay-verify="title" autocomplete="off" class="layui-input" id="finishTime">
                        </div>
                    </div>

                </div>

                <div class="layui-form-item">
                    <div class="layui-input-block">
                        <button class="layui-btn" lay-submit lay-filter="*">提交</button>
                    </div>
                </div>

            </div>

        </form>

    </div>
</div>
<script th:inline="none">
    layui.use(['jquery', 'form','layedit', 'laydate','layer'], function () {
        var $ = layui.jquery
            ,form = layui.form
            ,layStartTime = layui.laydate
            ,layFinishTime = layui.laydate
            ,layer = layui.layer;

        var eduTeacher = JSON.parse(sessionStorage.getItem("eduTeacher"));

        form.render();
        form.val('setCheckin',{
            "teacherName": eduTeacher.name,
        });

        //日期
        layFinishTime.render({
            elem: '#finishTime',
            type: 'time',
            value: new Date()
        });

        /*表单选项初始化*/

        //获取执教课程信息
        $.ajax({
            url: "http://localhost/api/teacher/courses",
            data: {"id" : eduTeacher.id},
            headers: {"token" : eduTeacher.token},
            type: "get",
            success: function (result) {
                if(result.code === 0){
                    var data = result.data;
                    var courseSelect = $("#course");
                    for(var i = 0; i < data.length; i++){
                        courseSelect.append("<option value='"+data[i].id+"'>"+data[i].name+"</option>")
                    }
                    form.render('select');
                }
            },
            error: function (result) {}
        })

        //获取教室信息
        $.ajax({
            url: "http://localhost/api/teacher/classrooms",
            data: {"id" : eduTeacher.id},
            headers: {"token" : eduTeacher.token},
            type: "get",
            success: function (result) {
                if(result.code === 0){
                    var data = result.data;
                    var classroomSelect = $("#classroom");
                    for(var i = 0; i < data.length; i++){
                        classroomSelect.append("<option value='"+data[i].id+"'>"+data[i].location+"</option>")
                    }
                    form.render('select');
                }
            },
            error: function (result) {}
        })

        //表单提交
        form.on('submit(*)', function (data) {
            //设置签到开始时间
            var currentTime = new Date();

            //设置签到结束时间
            //获取当前年月日
            var year = new Date().getFullYear();
            var month = new Date().getMonth();
            var date = new Date().getDate();
            data.field.finishtime = year+"-"+(month+1)+"-"+date+" "+data.field.finishtime;

            var finish = new Date(data.field.finishtime);

            if(currentTime >= finish){
                //时间间隔太短，或结束时间非法
                layer.msg("签到时间太短！");
                return false;
            }

            data.field.teacherId = eduTeacher.id;
            data.field.starttime = currentTime.Format("yyyy-MM-dd HH:mm:ss")
            data.field.status = 0


            console.log(data.field)

            $.ajax({
                url: "http://localhost/api/teacher/checkin",
                data: data.field,
                headers: {"token": eduTeacher.token},
                type:"post",
                success: function (){
                    layer.open({
                        type: 0,
                        content: '签到创建成功！',
                        yes: function () {
                            //刷新页面
                            window.location.reload();
                        }
                    });
                },
                error: function (result) {}
            })
            return false;
        });

    });

    Date.prototype.Format = function (fmt) {
        var o = {
            "M+": this.getMonth() + 1, //月份
            "d+": this.getDate(), //日
            "H+": this.getHours(), //小时
            "m+": this.getMinutes(), //分
            "s+": this.getSeconds(), //秒
            "q+": Math.floor((this.getMonth() + 3) / 3), //季度
            "S": this.getMilliseconds() //毫秒
        };
        if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
        for (var k in o)
            if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
        return fmt;
    }
</script>